package com.xiangxiangjd.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.google.common.collect.Lists;
import com.xiangxiangjd.analyse.bean.RepairStructure;
import com.xiangxiangjd.analyse.bean.SysDict;
import com.xiangxiangjd.analyse.controller.helper.$;
import com.xiangxiangjd.analyse.utils.excel.ExportExcel;
import com.xiangxiangjd.analyse.utils.excel.ImportExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xiangxiangjd.analyse.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.analyse.service.RepairMethodService;
import com.xiangxiangjd.analyse.bean.RepairMethod;
import com.xiangxiangjd.analyse.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-03-08
 */
@Controller
@RequestMapping("/analyse/repairMethod")
@Slf4j
public class RepairMethodController extends BaseController {
    @Autowired
    private RepairMethodService repairMethodService;


    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main() {
        return "RepairMethod/RepairMethodMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addRepairMethodHtml")
    public String addRepairMethodHtml() {
        return "RepairMethod/RepairMethodAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editRepairMethodHtml")
    public String editRepairMethodHtml(String id, Model model) {
        RepairMethod repairMethod = repairMethodService.selectRepairMethodByIdRepairMethod(id);


        //存入所有结构
        String str = repairMethodService.getSuperiorStructure(repairMethod.getStructureId());

        String[] split = str.split(",");

        List<String> ids = Arrays.asList(split);

        log.info(ids.size() + "结构数组：" + ids.toString());


        //结构
        model.addAttribute("allStructures", ids);


        model.addAttribute("repairMethod", repairMethod);
        return "/RepairMethod/RepairMethodEdit";
    }


    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showRepairMethodHtml")
    public String showRepairMethodHtml(String id, Model model) {


        RepairMethod repairMethod = repairMethodService.selectRepairMethodByIdRepairMethod(id);

        //查询 修理方法
        repairMethod.setStructureId(repairStructureService.getById(repairMethod.getStructureId()).getStructureName());


        repairMethod.setUpdateTime(StringToDate(repairMethod.getUpdateTime()));
        repairMethod.setCreateTime(StringToDate(repairMethod.getCreateTime()));


        model.addAttribute("repairMethod", repairMethod);
        return "RepairMethod/RepairMethodShow";
    }


    /**
     * 分页查询RepairMethod
     *
     * @param page         页码
     * @param limit        查几条
     * @param repairMethod 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findRepairMethodByPage")
    @ResponseBody
    public ResultPage findRepairMethodByPage(Integer page, Integer limit, RepairMethod repairMethod) {

        System.out.println("============================" + repairMethod.toString());


        return repairMethodService.findRepairMethodByPage(page, limit, repairMethod);
    }

    /**
     * 添加RepairMethod
     *
     * @param repairMethod 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addRepairMethod")
    @ResponseBody
    public ApiResultModel saveRepairMethod(RepairMethod repairMethod) {




        return repairMethodService.addRepairMethod(repairMethod);
    }

    /**
     * 编辑RepairMethod
     *
     * @param repairMethod 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editRepairMethod")
    @ResponseBody
    public ApiResultModel editRepairMethod(RepairMethod repairMethod) {
        return repairMethodService.editRepairMethod(repairMethod);
    }


    /**
     * 删除RepairMethod
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delRepairMethod", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delRepairMethod(@RequestParam("datas[]") ArrayList ids) {


        return repairMethodService.delRepairMethods(ids);
    }


    /**
     * 下载导入的ex 的表格
     */
    @ResponseBody
    @RequestMapping(value = "get/methodRtemplate")
    public void importFileTemplate(HttpServletResponse response) throws IOException {

        String fileName = "飞机修理任务级别分析统计系统.xlsx";
        List<RepairMethod> list = Lists.newArrayList();

        new ExportExcel("飞机修理任务级别分析统计系统", RepairMethod.class, 1).setDataList(list).write(response, fileName).dispose();


    }


    /**
     * 导入Excel   批量导入数据
     *
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Import")
    public ApiResultModel importFile(@RequestParam("file") MultipartFile file) {

        try {

            ImportExcel ei = new ImportExcel(file, 1, 0);


            List<RepairMethod> list = ei.getDataList(RepairMethod.class);


            //批量插入
            repairMethodService.addAll(list);


            // 导入成功
            return $.success();

        } catch (IOException e) {

        } catch (InvalidFormatException e) {

        } catch (IllegalAccessException e) {

        } catch (InstantiationException e) {

        }

        return $.error();
    }


}
